package com.carkeeper.user.controller;

import com.carkeeper.entity.Result;
import com.carkeeper.entity.StatusCode;
import com.carkeeper.user.pojo.Menu;
import com.carkeeper.user.service.MenuService;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName MenuController
 * @Description 菜单处理器
 * @Author yangbin
 * @Date 2020/11/23 14:43
 * @Version 1.0
 */

@RestController
@RequestMapping("/menu")
@Api(tags = "菜单管理接口api") //标签专门用来标注类的
@CrossOrigin
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 查询所有菜单
     * @return
     */
    @GetMapping("/findAll")
    @ApiOperation("查询所有列表")//标注方法
    public Result findAll() {
        List<Menu> menuList = menuService.findAll();
        if (menuList != null) {
            return new Result(true, StatusCode.OK, "查询成功", menuList);
        }
        return new Result(false, StatusCode.ERROR, "查询失败");
    }

    /**
     * 分页查询菜单
     * @return
     */
    @GetMapping("/queryPageByExample/{currentPage}/{pageSize}")
    @ApiOperation("条件分页查询")//标注方法
    public Result<Menu> queryPageByExample(@RequestParam(required = false) Map searchMap,
                                           @PathVariable("currentPage") Integer currentPage,
                                           @PathVariable("pageSize") Integer pageSize) {
        Page page = menuService.queryPageByExample(searchMap, currentPage, pageSize);
        if (page.getTotal() <= 0) {
            return new Result<>(false, StatusCode.ERROR, "查询失败");
        }
        return new Result<>(true, StatusCode.OK, "查询成功", page);
    }

    /**
     * 根据id查询菜单
     * @return
     */
    @GetMapping("/findById/{id}")
    @ApiOperation("根据id查询")//标注方法
    public Result findById(@PathVariable("id") Integer id) {
        Menu menu = menuService.findById(id);
        if (menu != null) {
            return new Result(true, StatusCode.OK, "查询成功", menu);
        }
        return new Result(false, StatusCode.ERROR, "查询失败");
    }


    /**
     * 新增菜单
     * @return
     */
    @PostMapping("/addMenu")
    @ApiOperation("新增菜单")//标注方法
    public Result addMenu(@RequestBody Menu menu) {
        int i = menuService.addMenu(menu);
        if (i == 1) {
            return new Result(true, StatusCode.OK, "新增成功", i);
        }
        return new Result(false, StatusCode.ERROR, "新增失败");
    }

    /**
     * 修改菜单
     * @return
     */
    @PutMapping("/updateMenu")
    @ApiOperation("修改菜单")//标注方法
    public Result updateMenu(@RequestBody Menu menu) {
        int i = menuService.updateMenu(menu);
        if (i == 1) {
            return new Result(true, StatusCode.OK, "修改成功", i);
        }
        return new Result(false, StatusCode.ERROR, "修改失败");
    }

    /**
     * 根据id删除菜单
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    @ApiOperation("删除菜单")//标注方法
    public Result deleteById(@PathVariable("id") Integer id) {
        int i = menuService.deleteById(id);
        if (i == 1) {
            return new Result(true, StatusCode.OK, "删除成功", i);
        }
        return new Result(false, StatusCode.ERROR, "删除失败");
    }
}
